package util;

import addressBook.Address;
import addressBook.Contact;
import sun.reflect.misc.ConstructorUtil;

import javax.swing.text.rtf.RTFEditorKit;
import java.io.*;
import java.net.URL;
import java.security.cert.PolicyNode;
import java.util.ArrayList;
import java.util.List;

/**
 * Read the contacts201112004206.txt file and return contacts list.
 *
 * @author Jarvan
 * @version 1.0
 * @create 2020/11/14 19:07
 */
public class ContactReaderUtil {
    public static List<Contact> getContactList() throws IOException {
        URL resource = ContactReaderUtil.class.getClassLoader().getResource("contacts201112004206.txt");
        BufferedReader bufferedReader = new BufferedReader(new FileReader(resource.getPath()), 1024 * 100);
        String title = bufferedReader.readLine();
        ArrayList<Contact> contacts = new ArrayList<>();
        while (bufferedReader.ready()){
            String readLine = bufferedReader.readLine();
            String[] split = readLine.split("\t{1,2}");
            String name = split[0] +" " +split[1];
            String street = split[2];
            String city = split[3];
            String state = split[4];
            String zipcode = split[5];
            String phone = split[6];
            contacts.add(new Contact(name,new Address(street,city,state,zipcode),phone));
        }
        return contacts;
    }
}
